/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package rowcounter;

import java.sql.*;


/**
 *
 * @author raj
 *
 */
public class Table {
    private String name="";
    private long rowCount = 0;
    private long queryTime=0;
    private String sqlBase="select count(*) from ";
    private Connection conn;
    private long start_msec=0, end_msec=0;

    public Table (String name) {
        this.name = name;
    }

    public Table (String name, Connection conn) {
        this.name = name;
        this.conn = conn;
    }

    public void runQuery() {
        try {
            String sqlQuery = new String(sqlBase+" "+this.name);

            Statement rowCountStatement = this.conn.createStatement();
            this.start_msec=System.currentTimeMillis();
            ResultSet resultSet = rowCountStatement.executeQuery(sqlQuery);
            while (resultSet.next()) {
                this.rowCount = Long.parseLong(resultSet.getString(1));
            }
            this.end_msec=System.currentTimeMillis();
            this.queryTime = (this.end_msec - this.start_msec);
            resultSet.close();
            rowCountStatement.close();

        }
        catch (Exception e) { e.printStackTrace(); System.exit(1); }
    }

    public String getName() {
        return this.name;
    }
    public long getQueryTime() {
        return this.queryTime;
    }
    public long getRowCount() {
        return this.rowCount;
    }
    public void setConnection(Connection conn) {
        this.conn = conn;
    }
}
